home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / banging_the_metal / charmode.text next >
Text File  |  2000-01-01  |  23KB  |  400 lines

  1. CHARMODE DEMO
  2.                 An Amiga Character mapped display mode generated by Custom Chips
  3.  
  4. FEATURES        Same-field update of the entire screen with beam avoidance
  5.                 Support for up to 640 x 287 resolution (currently PAL only)
  6.                 font support for up to 256 characters each up to 16 pixels tall
  7.                 Support for dynamic font changes from one field to the next
  8.  
  9. Remaps screens encoded as one byte per character into Amiga bitplane graphics.
  10.  
  11. This demo uses the copper to generate a custom screen mode and convert 
  12. character codes into graphic patterns from a font, by programming the blitter. 
  13. All processing is performed by the custom chips. All the CPU need do is write a 
  14. small table of character codes to determine the new display. Almost all the 
  15. main CPU time is available for other purposes.
  16.  
  17. This document is edited from the 'internal' development documentation, and 
  18. should be read in conjunction with the fuller explanations in Amiga Format.
  19.  
  20. CharMode is intended for use in emulators for vintage computers and terminals  
  21. with character-mapped displays. These include TRS-80 (64 x 16 characters of 8 x 
  22. 12 pixels, on a 512 x 192 pixel grid), CP/M, IBM MDA and most ASCII terminals 
  23. (typically 80 x 24 or 80 x 25 characters), PET (40x25 8 x 8 characters), ZX80 
  24. and ZX81 (32 x 24 characters), MSX, Dragon, Atari, etc.
  25.  
  26. It uses a font of 256 characters, each eight pixels wide and up to 16 pixels 
  27. tall, with one byte, left justified, in each pattern WORD. Thus the font takes 
  28. 256 (characters) x 32 (bytes) = 8K. If  the character height is less than 16 
  29. pixels the rendered character is truncated at the bottom.
  30.  
  31. Dynamic font changes are supported on a per-field basis by writing the changed 
  32. data to the corresponding font words. These need to be unpacked as the font 
  33. must use one WORD per character line (as it's a 16 bit blitter). Automatic 
  34. unpacking of 8 x 8 fonts is implemented.
  35.  
  36. Supported Screen formats:
  37.  
  38.   Char Width Char Height Chars/Row  Chars/Col     Prime Time  Characters
  39.  
  40.         8               8               80              24              58              1920
  41.         8               8               80              25              47              2000    
  42.         8               8               80              28              17              2240            
  43.         8               8               80              29              6               2320
  44.         8               10              80              24              14              1920
  45.         8               12              80              20              20              1600
  46.         8               16              80              16              8               1280
  47.         
  48.         8               8               64              32              40              2048
  49.         8               8               64              35              15              2240
  50.  
  51.         8               16              64              16              40              1024
  52.         8               12              64              16              52              1024
  53.         8               10              64              25              52              1600
  54.  
  55. Those formats are high resolution. Thsse are some low resolution formats:
  56.  
  57.         8               8               40              24              106             960
  58.         8               8               40              25              97              1000
  59.         8               8               40              32              40              1280
  60.         8               10              40              24              62              960
  61.         8               10              40              25              52              1000
  62.         8               12              40              16              108             640             
  63.         8               16              40              16              40              640
  64.  
  65.         8               8               32              24              106             768
  66.         8               8               32              32              40              1024
  67.         8               10              32              24              62              768
  68.         8               12              32              20              60              640     
  69.         8               16              32              16              40              512
  70.  
  71. These are all tested examples. Many other formats are possible; in general the 
  72. code expects widths of 32, 40, 64 or 80 characters and from 16 to 32 lines. 
  73. Other values may require additional ad hoc code to fine-tune display timing.
  74.  
  75. Notes on the formats
  76.  
  77. Char Heights are in Pixels. All character widths are eight pixels. Line widths 
  78. must be even as we're using a 16 bit chip set. Prime Time is the number of scan 
  79. lines of time available from the start of the field before the copper starts to 
  80. blit character patterns. Normally this is padded by adding HeadLines - scan 
  81. lines when the display is visible but not part of the blitted character map. 25 
  82. undisplayed scans are available at the start of each field. In practice one 
  83. additional line time should be allowed for the copper initialisation at the 
  84. start of each field.
  85.  
  86. It's advantageous to allow more lines, but this moves the character mapped 
  87. screen portion down the display. With 2000 8 x 8 pixel characters on-screen (25 
  88. lines of 80 columns) up to 87 lines of heading are supported (in PAL mode the 
  89. line limit is 287, and 25 x 8 = 200 pixels for the character map) and this 
  90. gives the latest allowable character map on the screen, with the maximum time 
  91. for updates at the start of the field - around 5500 microseconds.
  92.  
  93. High values of Prime Time indicate that the display could be moved higher up 
  94. the screen (rather than extending to display line 287) while still allowing 
  95. VBLANK updates. If the Prime Time value is low it may be necessary to start the 
  96. CPU update as the blitter finishes updating the last character and the last 
  97. line begins to be displayed - this boosts the available time from a 
  98. near-impossible eight lines to 24 with 80 x 16 8 x 16 characters.
  99.  
  100. Beam Avoidance is assured by timing the blits so that the last blit for the 
  101. last line is complete before that row of characters starts to be displayed. It 
  102. follows that character map or font updates can be performed at any time from 
  103. the start of display of that line, rather than top of frame. This is more 
  104. complicated than waiting for VBLANK - it requires a copper interrupt - but 
  105. gives more time for interrupt response and update before the data is needed for 
  106. the next field.
  107.  
  108. These modes are expected to be used on machines with fast memory, as much of 
  109. the chip RAM bus bandwidth is consumed by the copper and blitter updates. It 
  110. makes sense to keep the master copy of the current screen in fast RAM. If the 
  111. fast memory is more than twice the speed of chip RAM - as in the case of A3000 
  112. and A4000 systems, and expanded machines with processor-local 32 bit memory on 
  113. the CPU card - it is generally worth keeping a second copy of the last screen 
  114. written in fast RAM, and only writing changed information at the start of a 
  115. field. A similar technique can be used for font updates, as it's rare that the 
  116. entire font will change every field.
  117.  
  118. Word or long word transfers are important to ensure quick enough updates from 
  119. fast RAM to chip RAM. It may be worth adding a blitter pass to expand the font 
  120. from byte to word format. The trade-off is the extra chip bandwidth required 
  121. against the reduced time that the CPU spends waiting for writes to Chip RAM to 
  122. be completed.
  123.  
  124. HiRes character updates are performed with the BLITTER_NASTY bit set, consuming 
  125. all available processor memory cycles as each character is generated, but the 
  126. processor still gains access to memory at least four times per scan line as 
  127. WAITs are interspersed between the blits. There's much more bandwidth available 
  128. just before and after the vertical blank, and that is when writes to the font 
  129. and character map should be performed. Not only does the processor get more 
  130. time then, it's the ideal point at which to copy the emulated screen to the 
  131. real screen hardware to minimise delay and ensure field synchronisation, with 
  132. all characters changed in a given emulated field guaranteed to appear in the 
  133. next display.
  134.  
  135. The blitter and copper instructions used are compatible with all Amigas from 
  136. the original OCS through ECS to AGA. 30 KHz VGA monitor support could be but is 
  137. not included. Scan doubling would require lines to be generated twice as 
  138. quickly.
  139.  
  140. The current code is designed for a 15 KHz PAL display and would need changes 
  141. for other modes. It was developed and tested with Amiga Qdos, and is intended 
  142. to be used in custom screens on the Amiga, with ownership of the blitter 
  143. throughout updates, only relinquishing it when the emulator screen is swapped 
  144. off or paused.
  145.  
  146. The code sets out to centre the display vertically and horizontally, while 
  147. ensuring plenty of free time at the start of each field for updates from the 
  148. CPU. In total, the display must be at least 192 pixels tall and start by scan 
  149. 64 - well within limits of all Amiga chip sets.
  150.  
  151. Extra lines can be added at the top of the display. These are not used for the 
  152. character map but can be written (albeit slowly) by the processor and are ideal 
  153. for headings, menus and emulator status. They are added at the top to give the 
  154. blitter more time to generate the field without beam splitting. In order to 
  155. leave some bandwidth for the CPU, even in the middle of the bitmap, high 
  156. resolution 8 x 8 pixel character blits need slightly more blitter and copper 
  157. DMA time than they take to be output, so generation must start earlier.
  158.  
  159. For instance a display of the maximum number of characters (2320, in 80 columns 
  160. and 29 rows) requires at least 50 display scan lines of head-start to ensure 
  161. that all character lines are regenerated ahead of the beam. This comes close to 
  162. the limit of 29 x 8 = 232 pixel lines of text and 55 for the HeadLines. No more 
  163. than 30 line times are available at the start of the field for CPU screen 
  164. updates - less than 2 milliseconds.
  165.  
  166. That is not long, even when there's only about two and a quarter K that needs 
  167. to be written to update the entire screen. Allow for interrupt latency (up to 4 
  168. lines) & remember that Chip RAM bandwidth available to the CPU may be as low as 
  169. 1.8 Mb per second on many Amigas, even without DMA contention, so that a full 
  170. screen character write can take up to 20 scan lines to be completed! At best, 
  171. on Commodore 68030 or Phase Five accelerators, 5 lines are needed.
  172.  
  173. As of version 0.13, the code was not optimised to work out the best wait 
  174. between blits and ideal start line for LowRes characters, or HiRes characters 
  175. more than 12 pixels high. It may finish blitting long before the last line, 
  176. indicating that more time could be available between blits or (possibly) they 
  177. could all start later. The ad-hoc code to work out Prime Time only attempts to 
  178. maximise it for less tall characters, where the copper overhead for each blit 
  179. is more significant and keeping up with the beam is problematic.
  180.  
  181.  
  182. COPPER LIST COMPILER
  183.  
  184. The Copper list is generated by a SuperBASIC program, the Copper List Compiler. 
  185. This contains instructions to do the following things 50 times per second:
  186.  
  187. (1) Set up a custom bitplane display centred on the PAL screen
  188. (2) Expand an 8 x 8 pixel character font into internal format for blitting
  189. (3) Convert an 8 bit table of character codes into pattern blit instructions
  190. (4) Generate the new display by blitting one pattern for each character code
  191.  
  192. The Copper list Compiler (CharMode_bas) also calculates and displays the number 
  193. of the line on which blitting will start, so that the user can ensure CPU 
  194. updates will occur in time. Extra lines can be allocated to ensure beam 
  195. avoidance. It can include instructions to show the time taken for each activity 
  196. by colour-coding the screen in stripes.
  197.  
  198. The Compiler has been tested on systems ranging from AGA A4000/060 to ECS A500. 
  199. All supporting software is PD. It uses SuperBASIC extensions, all of which are 
  200. provided with the exception of Toolkit 2 or PD Toolkit. The BOOT program 
  201. assumes that Toolkit 2 is available.
  202.  
  203. Compilation parameters
  204.  
  205. To generate a custom copper list for a given display format, change the 
  206. assignment of these variables at the start of AutoChar_bas.
  207.  
  208. ShowTime        0 or 1  Flag - set to get coloured stripes on screen
  209.  
  210. Green: extracting even characters from the character map to the Copper List.
  211.  
  212. Blue: extracting even characters from the character map to the Copper List.
  213.  
  214. Red: ascending pass blitter unpack. CPU update can overlap if working forwards
  215.  
  216. Purple: descending pass of blitter unpack - changes in this phase get displayed
  217.         one field late - but blitter is not BUSY so CPU writes go at reasonable
  218.         speed.
  219.  
  220. Dark Blue: rendering characters by blitting patterns from the font. This area
  221.         should end before the start of display of the last character line.
  222.  
  223. Blitfont 0 or 1 Flag - expand 8 x 8 font to 8 x 16 automatically every field?
  224.  
  225. This is too slow to be feasible with a full screen HighRes display, but can be 
  226. performed on a partial-height screen if HeadLines are allocated to give time 
  227. for the update at the start of each field. It only works with an 8 x 8 
  228. character font.
  229.  
  230. BlitChars       0 or 1  Flag - include code to expand the CharMap?
  231.  
  232.         Otherwise to change a character you need to poke a word giving the base 
  233. address of its pattern in the font into the copper list at a corresponding 
  234. position. For instance with an 8 pixel tall font, to display character 65 
  235. (ASCII "A") you'd poke 65 x16 (bytes per character) into the copper list. 
  236. Multiply by 32 for characters up to 16 pixels tall.
  237.  
  238. The characters are controlled by words between the pointers FirstCharMove and
  239. LastCharMove (both absolute addresses). The first character's font pointer 
  240. word is at FirstCharMove. The second is FOUR BYTES EARLIER. The third is 20 
  241. bytes on from the first, the fourth is at offset 16 from the start, and so on:
  242.  
  243. FirstCharMove,FirstCharMove-4, 20, 16, 40, 36, ... LastCharMove+4,LastCharMove
  244.  
  245. CharHeight%     1 to 16 Height of each character in pixels
  246.  
  247.         Typically 8, 9, 10, 12 or 16. fonts 8, 9 and 16 pixels high are
  248.         included. The tallest uses IBM PC character codes, the shortest follows
  249.         the MGT SAM convention with duplicates of the ASCII set 32-127 in the
  250.         second half (ignoring bit 7) while the nine pixel high one is the 256
  251.         character Minerva Qdos set.
  252.  
  253. CharWidth%      8               THIS CANNOT BE CHANGED!
  254.  
  255. CharColumns%    32,40,64,80     Number of characters on each display line
  256.  
  257.         High Resolution (70 nS pixel) mode is used for widths over 40
  258.         characters (320 pixels)
  259.         Other even values also work but may not be optimally timed. 
  260.         Odd values are not allowed, for reasons of 16 bit alignment.
  261.  
  262.  
  263. CharLines% 1 to 35      Number of character lines
  264.  
  265. HeadLines% 0 to 279 Number of pixel lines in bitplane BEFORE characters start
  266.  
  267. These is an interaction between these two. When a large number of CharLines% is 
  268. chosen, the blitter needs more time to render them, so HeadLines% may be 
  269. required to delay the display of characters until the blitter has had time to 
  270. render them. This has the unfortunate effect of pushing the display down the 
  271. screen, but does give some extra space for menus or headings at the top.
  272.  
  273. There's no reason why the HeadLines should not be in a different mode, although 
  274. the compiler assumes one bit map for the characters and heading. The important 
  275. thing is to delay output of the characters to give time for CPU update of the 
  276. CharMap and font, Copper and Blitter unpacking of those into more Copper and 
  277. Blitter data, and ensure that rendering of the characters still precedes the 
  278. beam!
  279.  
  280. The compiled Copper list uses absolute addresses but can be relocated to any 
  281. 64K boundary by adjusting the pointers to PAGE, set at or fairly near the 
  282. start. The page alignment is vital because the font must be at the start of a 
  283. page so that it can be efficiently indexed with scaled character codes from 
  284. zero in the low word of blitter pointers.
  285.  
  286. Compiler Limitations
  287.  
  288. This section discusses the limits imposed by the choice of data structures and 
  289. design of the Copper List Compiler. These are practical limits associated with 
  290. the current implementation on  OCS Amiga hardware, rather than theoretical 
  291. limits of the CharMode principle.
  292.  
  293. The current version of the compiler (0.13) only supports dynamic font 
  294. unpacking for 8 x 8 character fonts. This suits all the machines (Atari, C64, 
  295. MSX, Sinclair ZX-81) which require it. Taller fonts are supported but not 
  296. dynamic. In other words modified bytes must be written unpacked (16 bits wide, 
  297. left justified). The unpacking consumes quite a lot of blitter time - too much 
  298. for full-screen HiRes display. It's fine for displays of up to about 1000 
  299. characters, and again that corresponds to the displays we'd want to emulate.
  300.  
  301. The compiler assumes a 50 Hertz PAL display with up to 287 lines. NTSC and 
  302. Double-scan VGA modes could be supported but with reduced performance as the 
  303. display pushes out more pixels for a given amount of time available for blitter 
  304. activity.
  305.  
  306. Characters must be eight pixels wide. This is a consequence of the way the 
  307. blitter is used to render two characters at once. Peter Corlett had this key 
  308. idea, without which the code would be much slower as it would need to mask and 
  309. generate one character at a time. It's easy to render 16-pixel wide characters 
  310. at fair speed with simple un-masked blits, but in general other widths would be 
  311. rendered much more slowly.
  312.  
  313. These modes all use a single bitplane and two colours. This minimise Amiga Chip 
  314. RAM and bandwidth requirements, and suits the old monochrome screens which were 
  315. standard for character-mapped modes.
  316.  
  317. Colour character-mapped modes could supported analogously but need a different 
  318. approach as all output goes to a single bitplane. This can be re-coloured 
  319. efficiently with a copper list, but the extra copper instructions to change 
  320. colours must be fitted around the existing heavy copper activity. Attribute 
  321. plus bitplane graphics (as on C64, MSX, SAM, Spectrum and TI-99/4) can be 
  322. emulated similarly, although the heavy copper-blitting characteristic of 
  323. character-mapped emulation is not needed in such cases.
  324.  
  325.                         Simon N Goodwin, simon@studio.woden.com, January 2000
  326.  
  327. DATA STRUCTURES
  328.  
  329. The CharMode data structures start on a 64K boundary in Chip memory. font and 
  330. Bitplane data is guaranteed to fit in the first 64K, while the Copper list will 
  331. start there but may extend into the next 64K page.
  332.  
  333. font                   size:   8K (allocated - may not be all used)
  334.  
  335. If  LoRes 8 x 8 characters are in use the font is stored in two forms. At 
  336. offset +4096 there is a 2K packed byte image, as would be used in an 8 bit 
  337. micro, with 8 x 256 bytes per character. This is immediately preceded by a 
  338. blitter-generated 4K version, identical but that each packed byte is followed 
  339. by a zero byte for convenience when performing 16 bit blits. The packed data is 
  340. followed by 2K of unused space. To change the font, the CPU simply writes the 
  341. changes into the packed area and they are unpacked and used automatically by 
  342. the copper and blitter.
  343.  
  344. If HiRes characters are in use or the character height is not eight, the font 
  345. is only stored in unpacked form with 16 by 16 bits per character. Again valid 
  346. pixel data is only in the eight most significant bits of each word, but each 
  347. character supports up to 16 words. If the font needs to be changed on the fly 
  348. the CPU must store new data bytes at unpacked word intervals, as unpacking is 
  349. only automatic for LowRes 8 x 8 characters.
  350.  
  351. For ease of addressing the number of rows in each character should be a power 
  352. of two even if the number of rows used is not - e.g. 16 for 10, 12 or 16 pixel 
  353. tall characters, and eight for characters up to 8 pixels tall.
  354.  
  355. The order of characters corresponds to the character code from 0 to 255, and 
  356. need not be standard ASCII - it could be a variant like PET or TRS-80 ASCII or 
  357. something entirely different, such as ZX-80 or ZX-81 character codes. You can 
  358. switch between two fonts at the same Chip address MOD 65536 with just two 
  359. POKEs to the copper list, setting BLTAPT.H and BPLBPT.H to point to the 
  360. required page.
  361.  
  362. Bitplane sizes: 12K for 64x16 characters of 8x12 pixels each (HiRes 512 x 192)
  363.                8000 bytes for 40 x 25 8 x 8 pixel characters (LoRes 320 x 200)
  364.  
  365. A conventional Amiga bit map with one word for 16 pixels and lines consecutive 
  366. in memory. Allow for HeadLines and border pixels, if any, adding to the size of 
  367. the bitplane. There is a gap of 128 bytes at the end of the bitplane before the 
  368. font data starts.
  369.  
  370. Character Map   size:   1K to 2K typically.
  371.  
  372. One byte for each character to be displayed, as a linear array with no gaps 
  373. between lines, e.g. the data for the second line of a 32 column x 16 line 
  374. screen starts with the 33rd byte. These bytes are shifted by four or five 
  375. places and inserted into the Copper List. In modes with no more than 1024 
  376. characters this is automatically unpacked into the Copper list before each new 
  377. field is displayed.
  378.  
  379. Copper List: A few hundred bytes of overhead + 20 bytes per character to blit
  380.  
  381. This is the clever bit. It contains anything up to 45K of Copper instructions 
  382. which must be executed every field (i.e. 50 times per second). These start by 
  383. setting up the display, but most of their effort goes into programming the 
  384. blitter to read the font for each byte in the character map and copy it to the 
  385. correct place in the Bit Map. Optional coloured stripes can show the time when 
  386. the copper is busiest and confirm beam avoidance without double buffering.
  387.  
  388. If  automatic font expansion is in use then the blitter unpacks the 8 x 8 
  389. pixel font patterns into 8 x 16 bit words before it starts to blit characters. 
  390. Unpacking is performed in two passes, ascending and descending. The blitter 
  391. avoids hogging the bus (not 'nasty') so the CPU can finish off font writes 
  392. without falling behind the beam, at least during the ascending pass. If it's 
  393. still writing during the descending pass, those updates will not appear in the 
  394. unpacked font till the next field.
  395.  
  396. The blitter is switched into 'nasty' mode for the individual HiRes character 
  397. blits, which are short and well spaced so the CPU can get through between them 
  398. - although it will have to wait much more than if it got its writes done 
  399. earlier in the field. In general, the earlier the better.
  400.